# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the PreferredEmail Bugzilla Extension.
#
# The Initial Developer of the Original Code is IBM LTC
# Portions created by the Initial Developer are Copyright (C) 2010 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Tiago Rodrigues de Mello <timello@linux.vnet.ibm.com>

package Bugzilla::Extension::PreferredEmail;
use strict;
use base qw(Bugzilla::Extension);

use Bugzilla::Util;
use Bugzilla::Error;

use Bugzilla::Extension::PreferredEmail::Util;
use Bugzilla::Extension::PreferredEmail::Constants;

our $VERSION = '0.01';

sub auth_verify_methods {
    my ($self, $args) = @_;
    my $modules = $args->{modules};
    if (exists $modules->{LDAP}) {
        $modules->{LDAP} = 'Bugzilla/Extension/PreferredEmail/Auth/Verify/LDAP.pm';
        $modules->{DB}   = 'Bugzilla/Extension/PreferredEmail/Auth/Verify/DB.pm';
    }
}

sub template_before_create {
    my ($self, $args) = @_;
    my $config = $args->{'config'};
    $config->{VARIABLES}->{SendBugMail} = sub {
        my ($id, $mailrecipients) = (@_);
        require Bugzilla::BugMail;
        my $mail = Bugzilla::BugMail::Send($id, $mailrecipients);
        my @sent;
        foreach my $recipient (@{$mail->{sent}}) {
            my $preferred_email = get_preferred_email($recipient);
            push @sent, $recipient . " (via $preferred_email)";
        }
        $mail->{sent} = \@sent; 
        return $mail;
    };
}

sub template_before_process {
    my ($self, $args) = @_;
    my $user = Bugzilla->user;

    my ($vars, $file, $context) = @$args{qw(vars file context)};

    if ($file eq 'account/prefs/account.html.tmpl') {
        if (defined $vars->{changes_saved}) {
            # $context->stash cannot be called, otherwise it causes recursion.
            my $params = $context->{STASH}->{cgi};
            my $preferred_email = $params->param('preferred_email');
            trick_taint($preferred_email);
            validate_preferred_email($preferred_email);
            Bugzilla->dbh->do("UPDATE profiles SET extern_id = ? WHERE userid = ?",
                              undef, ($preferred_email, $user->id));
        }
        my $login_name = Bugzilla->user->login;
        $vars->{'preferred_email'} = get_preferred_email($login_name);
    }
}

sub mailer_before_send {
    my ($self, $args) = @_;
    my $email = $args->{email};
    my $login_name = $email->header('To');
    my $preferred_email = get_preferred_email($login_name) || $login_name;
    $email->header_set('To', $preferred_email);
}


__PACKAGE__->NAME;
